﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Project_KhuyenMaiVang.DataProvider;
using Project_KhuyenMaiVang.Models;
using System.Data.SqlClient;
using System.Data;

namespace Project_KhuyenMaiVang.DAO
{
    public class ThongTinNhanHangDAO : MyDataProvider
    {
        // Thêm 1 thông tin nhận hàng
        // parameter    : ThongTinNhanHangDTO
        // return true nếu thành công, false nếu thất bại
        public bool insert(ThongTinNhanHangDTO product)
        {
            bool result = true;
            try
            {
                string strSql = "INSERT INTO THONGTINNHANHANG (ID, HoTen, DiaChi, DienThoai, CMND) VALUES (@ID, @HoTen, @DiaChi, @DienThoai, @CMND)";
                _Command = new SqlCommand(strSql, _Connection);
                SqlParameter[] param = new SqlParameter[5];

                param[0] = new SqlParameter("@ID", SqlDbType.Int);
                param[1] = new SqlParameter("@HoTen", SqlDbType.NVarChar, 50);
                param[2] = new SqlParameter("@DiaChi", SqlDbType.NVarChar, 50);
                param[3] = new SqlParameter("@DienThoai", SqlDbType.NVarChar, 13);
                param[4] = new SqlParameter("@CMND", SqlDbType.NVarChar, 10);

                param[0].Value = product.ID;
                param[1].Value = product.HoTen;
                param[2].Value = product.DiaChi;
                param[3].Value = product.DienThoai;
                param[4].Value = product.CMND;

                for (int i = 0; i < param.Length; i++)
                {
                    _Command.Parameters.Add(param[i]);
                }

                _Command.CommandType = CommandType.Text;
                _Command.ExecuteNonQuery();
            }
            catch
            {
                result = false;
            }

            return result;
        }


        /// <summary>
        /// Lấy danh sách tất cả các thông tin nhận hàng
        /// </summary>
        /// <param name="IDAccount"></param>
        /// <returns>List ThongTinNhanHangDTO</returns>
        public List<ThongTinNhanHangDTO> GetListThongTinNhanHangDTO()
        {
            List<ThongTinNhanHangDTO> result = new List<ThongTinNhanHangDTO>();
            try
            {
                string QueryString = @"select * from ThongTinNhanHang";
                _Adapter = new SqlDataAdapter(QueryString, _Connection);
                DataSet dataset = new DataSet();
                _Adapter.Fill(dataset);
                
                DataTable dt = dataset.Tables[0];

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    object ob = GetDataFromDataRow(dt, i);
                    result.Add((ThongTinNhanHangDTO)ob);
                }

            }
            catch (System.Exception ex)
            {
                return null;
            }
            return result;
        }


        protected override object GetDataFromDataRow(System.Data.DataTable dt, int i)
        {
            ThongTinNhanHangDTO thongTinNhanHangDTO = new ThongTinNhanHangDTO();

            thongTinNhanHangDTO.ID = Int32.Parse(dt.Rows[i]["ID"].ToString());
            thongTinNhanHangDTO.HoTen = dt.Rows[i]["HoTen"].ToString();
            thongTinNhanHangDTO.DiaChi = dt.Rows[i]["IDAccount"].ToString();
            thongTinNhanHangDTO.DienThoai = dt.Rows[i]["TenSanPham"].ToString();
            thongTinNhanHangDTO.CMND = dt.Rows[i]["MoTaChiTiet"].ToString();


            return (object)thongTinNhanHangDTO;
        }
    }
}